The code chunk below uses p_load() of pacman package to check if packages are installed in the computer. If they are, then they will be launched into R. The R packages installed are:
plotly, R library for plotting interactive statistical graphs.
gganimate, an ggplot extension for creating animated statistical graphs.
DT provides an R interface to the JavaScript library DataTables that create interactive table on html page.
tidyverse, a family of modern R packages specially designed to support data science, analysis and communication task including creating static statistical graphs.
gifski converts video frames to GIF animations using pngquant’s fancy features for efficient cross-frame palettes and temporal dithering. It produces animated GIFs that use thousands of colors per frame.
gapminder: An excerpt of the data available at Gapminder.org. We just want to use its country_colors scheme.
package 'ggiraph' successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\kellyzhao\AppData\Local\Temp\RtmpqobjAn\downloaded_packages
package 'plotly' successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\kellyzhao\AppData\Local\Temp\RtmpqobjAn\downloaded_packages
package 'lpSolve' successfully unpacked and MD5 sums checked
package 'transformr' successfully unpacked and MD5 sums checked
package 'gganimate' successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\kellyzhao\AppData\Local\Temp\RtmpqobjAn\downloaded_packages
package 'DT' successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\kellyzhao\AppData\Local\Temp\RtmpqobjAn\downloaded_packages
package 'gifski' successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\kellyzhao\AppData\Local\Temp\RtmpqobjAn\downloaded_packages
package 'gapminder' successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\kellyzhao\AppData\Local\Temp\RtmpqobjAn\downloaded_packages
package 'rPackedBar' successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\kellyzhao\AppData\Local\Temp\RtmpqobjAn\downloaded_packages
Note: Background color is now white and the font color is red and bold
2.1.4 Displaying statistics on tooltip
In this example, a function is used to compute 90% confident interval of the mean. The derived statistics are then displayed in the tooltip. This is created by creating a function
Interactivity: Elements associated with a data_id (i.e CLASS) will be highlighted upon mouse over. Note that the default value of the hover css is hover_css = “fill:orange;”.
2.1.6 Customising hover effect style
Using css declaration to change the highlighting effect
Interactivity: Elements associated with a data_id (i.e CLASS) will be highlighted upon mouse over. Notice opts_hover refers to the selected data and opts_hover_inv refers to the non-selected data. Different from section 2.1.3 above, the css customisation request are encoded directly.
Interactivity: Web document link with a data object will be displayed on the web browser upon mouse click. Note that click actions must be a string column in the dataset containing valid javascript instructions.
2.1.9 Coordinated multiple views with ggiraph
Coordinated multiple views methods is interactive in which when a data point of one of the dotplot is selected, the corresponding data point ID on the second data visualisation will be highlighted too.
In order to build a coordinated multiple views, the following programming strategy will be used:
Appropriate interactive functions of ggiraph will be used to create the multiple views.
patchwork function of patchwork package will be used inside girafe function to create the interactive coordinated multiple views.
The data_id aesthetic is critical to link observations between plots and the tooltip aesthetic is optional but nice to have when mouse over a point.
2.2 Using plotly method for interactive data visualization
Plotly’s R graphing library create interactive web graphics from ggplot2 graphs and/or a custom interface to the (MIT-licensed) JavaScript library plotly.js inspired by the grammar of graphics.
Different from other plotly platform, plot.R is free and open source.
There are two ways to create interactive graph by using plotly, they are:
Click on a data point of one of the scatterplot and see how the corresponding point on the other scatterplot is selected.
2.3 Using crosstalk method for interactive data visualization
Crosstalk is an add-on to the htmlwidgets package. It extends htmlwidgets with a set of classes, functions, and conventions for implementing cross-widget interactions (currently, linked brushing and filtering).
Data objects in R can be rendered as HTML tables using the JavaScript library ‘DataTables’ (typically via R Markdown or Shiny).
Code
DT::datatable(exam_data, class="compact")
2.3.2 Linked brushing using crosstalk method
Things to learn from the code chunk:
highlight() is a function of plotly package. It sets a variety of options for brushing (i.e., highlighting) multiple plots. These options are primarily designed for linking multiple plotly graphs, and may not behave as expected when linking plotly to another htmlwidget package via crosstalk. In some cases, other htmlwidgets will respect these options, such as persistent selection in leaflet.
bscols() is a helper function of crosstalk package. It makes it easy to put HTML elements side by side. It can be called directly from the console but is especially designed to work in an R Markdown document. Warning: This will bring in all of Bootstrap!.
gganimate extends the grammar of graphics as implemented by ggplot2 to include the description of animation. It does this by providing a range of new grammar classes that can be added to the plot object in order to customise how it should change with time.
transition_*() defines how the data should be spread out and how it relates to itself across time.
view_*() defines how the positional scales should change along the animation.
shadow_*() defines how data from other points in time should be presented in the given point in time.
enter_*()/exit_*() defines how new data should appear and how old data should disappear during the course of the animation.
ease_aes() defines how different aesthetics should be eased during transitions.
Import data from the Data worksheet from GlobalPopulation Excel workbook.
Code
col <-c("Country", "Continent")globalPop <-read_xls("data/GlobalPopulation.xls",sheet="Data") %>%mutate_each_(funs(factor(.)), col) %>%mutate(Year =as.integer(Year))
Basic ggplot function to create static bubble plot
Appropriate ggplot2 functions are used to create a static bubble plot. The output is then saved as an R object called gg.
ggplotly() is then used to convert the R graphic object into an animated svg object.
Notice that although show.legend = FALSE argument was used, the legend still appears on the plot. To overcome this problem, theme(legend.position='none') should be used as shown in the plot and code chunk below.